﻿using CsvHelper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kimd.Common
{
    public class CsvOperator<T> where T : ICsv, new()
    {
        public void WriteCSV(string fileName, List<T> TValue, out string errInfo)
        {
            errInfo = "";
            try
            {
                bool isFirst = true;
                using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
                {
                    using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
                    {
                        CsvWriter csv = new CsvWriter(sw);
                        csv.Configuration.HasHeaderRecord = true;
                        foreach (T value in TValue)
                        {
                            if (isFirst)
                            {
                                foreach (KeyValuePair<string, string> item in (value as ICsv).CsvName2Property)
                                {
                                    csv.WriteField(item.Key);
                                }
                                csv.NextRecord();
                            }
                            foreach (var item in (value as ICsv).CsvName2Property.Values)
                            {
                                object val = value.GetType().GetProperty(item).GetValue(value, null);
                                if (val != null)
                                    csv.WriteField(val.ToString());
                            }
                            csv.NextRecord();
                            isFirst = false;
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                errInfo = ex.Message;
            }
        }
        public void ReadCSV(string fileName, out List<T> TValue, out string errInfo)
        {
            errInfo = "";
            TValue = new List<T>();
            try
            {
                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    using (StreamReader sw = new StreamReader(fs, Encoding.Default))
                    {
                        CsvReader csv = new CsvReader(sw);
                        csv.Configuration.HasHeaderRecord = true;

                        while (csv.Read())
                        {
                            T value = new T();
                            try
                            {
                                foreach (KeyValuePair<string, string> item in (value as ICsv).CsvName2Property)
                                {
                                    object v = Convert.ChangeType(csv.GetField<string>(item.Key), value.GetType().GetProperty(item.Value).PropertyType);
                                    value.GetType().GetProperty(item.Value).SetValue(value, v, null);
                                }
                            }
                            catch
                            {
                                continue;
                            }
                            TValue.Add(value);
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                errInfo = ex.Message;
            }
        }
    }
}
